package com.efruit.poadapters;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.efruit.dao.IProductDAO;
import com.efruit.po.Product;
import com.efruit.shopcart.IProductInfo;
import com.efruit.shopcart.IProductInfoFetcher;

public class SCProductInfoFetcherImpl implements IProductInfoFetcher {
	private IProductDAO productDAO;

	public IProductDAO getProductDAO() {
		return productDAO;
	}

	public void setProductDAO(IProductDAO productDAO) {
		this.productDAO = productDAO;
	}

	@Override
	public IProductInfo getProductInfo(int id) {
		
		Product product = productDAO.findById(id);
		if (product != null) {
			ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
					"applicationContext.xml");
			IProductInfo productInfo = (IProductInfo)ctx.getBean("ProductInfoImpl");
			productInfo.setId(product.getProductId());
			System.out.println("fetcher:"+productInfo.getId());
			productInfo.setName(product.getName());
			productInfo.setPrice(product.getPrice());
			return productInfo;
		}
		return null;
	}

}
